<?php
# $Id:
require_once(APP_LIB_DIR . 'BaseModel.class.php');
Class UserManagementModel Extends BaseModel
{

	function add_appuser($detail)
    {
    	$this->insert('appuser', $detail);
        return $this->get_last_insert_id('appuser');
    }
	function add_billing_info($detail)
    {
    	$this->insert('mod_billing_info', $detail);
        return $this->get_last_insert_id('mod_billing_info');
    }
	function add_shipping_info($detail)
    {
    	$this->insert('mod_shipping', $detail);
        return $this->get_last_insert_id('mod_shipping');
    }

    function update_appuser($id, $detail)
    {
        return $this->update('appuser', $detail, "id = '$id'");
    }
    
    function update_billing_info($id, $detail)
    {
        return $this->update('mod_billing_info', $detail, "user_id = '$id'");
    }
    
    function update_shipping_info($id, $detail)
    {
        return $this->update('mod_shipping', $detail, "user_id = '$id'");
    }

	function delete_appuser($id)
	{
    	$query = "UPDATE appuser SET active='N' WHERE id=$id";
        return $this->query($query, false);
	}

	function get_details($id)
	{
    	$query = "SELECT shp.lastname as shp_lname,shp.firstname as shp_fname,shp.middle_initial as shp_mname,
			shp.address1 as shp_add1,shp.address2 as shp_add2,shp.city as shp_city,
			shp.state as shp_state,shp.zipcode as shp_zip,shp.country as shp_country,
			shp.phone as shp_phone,shp.fax as shp_fax,shp.cell as shp_cell,
			bll.*,
			CONCAT(bll.cc_exp_mo,'/',cc_exp_yr) as exp_date,appuser.*, usertype.usertype_name as type, appuser.privilege as type_id 
			FROM appuser 
			LEFT JOIN usertype ON appuser.privilege=usertype.id 
			LEFT JOIN mod_billing_info bll ON appuser.id = bll.user_id 
			LEFT JOIN mod_shipping shp On appuser.id = shp.user_id
			WHERE appuser.active='Y' 
			AND appuser.id='$id'";
        return $this->query($query, true);
	}

	function check_billing_info($id)
	{
    	$query = "SELECT * FROM mod_billing_info WHERE user_id='$id'";
        return $this->query($query, true);
	}
	function check_shipping_info($id)
	{
    	$query = "SELECT * FROM mod_shipping WHERE user_id='$id'";
        return $this->query($query, true);
	}

	function count_appuser()
	{
		$query = "SELECT COUNT(*) FROM appuser WHERE active='Y'";
		$num = $this->query($query, true);
        return $num[0][0];
	}

	function get_list($offset, $limit)
	{
		$query = "SELECT mod_billing_info.*,appuser.*,
	                        usertype.usertype_name as privilege,
        	                appuser.privilege as type_id
                	  FROM appuser
	                  LEFT JOIN usertype ON appuser.privilege=usertype.id
			  LEFT JOIN mod_billing_info ON appuser.id = mod_billing_info.user_id
        	          WHERE appuser.active='Y' LIMIT $offset, $limit";
        return $this->query($query, true);
	}
       
	function usertypelist ()
	{
        	$query = 'SELECT * FROM usertype';
        return $this->query($query,true);
	}


}
?>
